package com.hy.study.reduce;

import com.hy.study.bean.FlowBean;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author huyong
 * @date 2024 年 01 月 29 日 16:47: 44
 */
public class FlowCountReducer extends Reducer<Text, FlowBean, Text, FlowBean> {
    private final FlowBean flowBean = new FlowBean();

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Reducer<Text, FlowBean, Text, FlowBean>.Context context) throws IOException, InterruptedException {
        long upFlowSum = 0L;
        long downFlowSum = 0L;
        long _flowSum = 0L;
        for (FlowBean value : values) {
            upFlowSum += value.getUpFlow();
            downFlowSum += value.getDownFlow();
            _flowSum += value.getSumFlow();
        }
        flowBean.setSumFlow(_flowSum);
        flowBean.setUpFlow(upFlowSum);
        flowBean.setDownFlow(downFlowSum);
        context.write(key, flowBean);
    }
}
